package com.atguigu1.core.action

import org.apache.spark.rdd.RDD
import org.apache.spark.{SparkConf, SparkContext}

/**
 *
 * @description: 行动算子案例
 * @time: 2021-03-12 11:45
 * @author: baojinlong
 **/
object Spark03Aggregate {
  def main(args: Array[String]): Unit = {
    val conf: SparkConf = new SparkConf().setMaster("local[*]").setAppName("rdd")
    // 设置rdd分区数字
    val sparkContext = new SparkContext(conf)
    val rdd: RDD[Int] = sparkContext.makeRDD(Seq(1, 2, 3, 4), numSlices = 2)
    // 初始值,分区内计算规则,分区间计算规则, aggregateByKey的初始值只会参与到分区内计算,而aggregate则参与到分区内和分区间.
    // 10+1+2 10+3+4  10 + 13 + 17
    println(rdd.aggregate(0)(_ + _, _ + _))
    // 如果分区内和分区间的计算规则一样可以用fold
    println(rdd.fold(0)(_ + _))
    sparkContext.stop()
  }
}
